/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.utils.SystemLogUtil;
import com.jeesite.modules.sys.entity.Log;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.kj.entity.KjContributeProject;
import com.jeesite.modules.kj.service.KjContributeProjectService;

import java.util.List;

/**
 * kj_contribute_projectController
 * @author hzy
 * @version 2021-07-13
 */
@Controller
@RequestMapping(value = "${adminPath}/kj/kjContributeProject")
public class KjContributeProjectController extends BaseController {

	@Autowired
	private KjContributeProjectService kjContributeProjectService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public KjContributeProject get(String id, boolean isNewRecord) {
		return kjContributeProjectService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("kj:kjContributeProject:view")
	@RequestMapping(value = {"list", ""})
	public String list(KjContributeProject kjContributeProject, Model model) {
		model.addAttribute("kjContributeProject", kjContributeProject);
		return "modules/kj/kjContributeProjectList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("kj:kjContributeProject:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<KjContributeProject> listData(KjContributeProject kjContributeProject, HttpServletRequest request, HttpServletResponse response) {
		kjContributeProject.setPage(new Page<>(request, response));
		Page<KjContributeProject> page = kjContributeProjectService.findPage(kjContributeProject);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("kj:kjContributeProject:view")
	@RequestMapping(value = "form")
	public String form(KjContributeProject kjContributeProject, Model model) {
		model.addAttribute("kjContributeProject", kjContributeProject);
		return "modules/kj/kjContributeProjectForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("kj:kjContributeProject:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated KjContributeProject kjContributeProject, HttpServletRequest request) {
		if(kjContributeProject.getIsNewRecord()){
			SystemLogUtil.insertAdminOperate(UserUtils.getUser(), "项目管理", Log.TYPE_UPDATE, "创建项目", kjContributeProject.getName(), request);
		}
		kjContributeProjectService.save(kjContributeProject);
		return renderResult(Global.TRUE, text("保存捐赠项目成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("kj:kjContributeProject:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(KjContributeProject kjContributeProject, HttpServletRequest request) {
		SystemLogUtil.insertAdminOperate(UserUtils.getUser(), "项目管理", Log.TYPE_UPDATE, "删除项目", kjContributeProject.getName(), request);
		kjContributeProjectService.delete(kjContributeProject);
		return renderResult(Global.TRUE, text("删除捐赠项目成功！"));
	}

	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("kj:kjContributeProject:view")
	@RequestMapping(value = "kjContributeProjectSelect")
	public String empUserSelect(KjContributeProject kjContributeProject, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("KjContributeProject", kjContributeProject);
		return "modules/kj/kjContributeProjectSelect";
	}


	/**
	 * 导出捐赠项目数据
	 */
	@RequiresPermissions("kj:kjContributeProject:view")
	@RequestMapping(value = "exportData")
	public void exportData(KjContributeProject kjContributeProject, Boolean isAll, String ctrlPermi, HttpServletResponse response) {

		kjContributeProject.setPageNo(null);
		kjContributeProject.setPageSize(null);
		List<KjContributeProject> list = kjContributeProjectService.findList(kjContributeProject);
		String fileName = "捐赠项目数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport("捐赠项目数据", KjContributeProject.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}
}
